package com.yyb.utils.neteaseim.api.impl;

import com.jiuqian.xdpx.aid.neteaseim.api.NeImUserService;
import com.jiuqian.xdpx.aid.neteaseim.bean.entity.NeImFriendBean;
import com.jiuqian.xdpx.aid.neteaseim.bean.entity.NeImUser;
import com.jiuqian.xdpx.aid.neteaseim.bean.reseponse.NeImBaseResponse;
import com.jiuqian.xdpx.aid.neteaseim.bean.reseponse.NeImRelationshipResponse;
import com.jiuqian.xdpx.aid.neteaseim.bean.reseponse.NeImUserInfoResponse;
import com.jiuqian.xdpx.core.util.LongUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Date;

@Service
public class NeImUserServiceImpl extends NeImBaseServiceImpl implements NeImUserService {


    @Override
    public NeImBaseResponse addddFriend(NeImFriendBean bean) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("accid", bean.getAccid());
        params.add("faccid", bean.getFaccid());
        // restTemplate.postForObject()只能传递String类型参数
        params.add("type", Integer.toString(bean.getType()));
        if (StringUtils.isNotBlank(bean.getMsg())) {
            params.add("msg", bean.getMsg());
        }
        NeImBaseResponse responseBean = super.build(params, ADD_FRIEND, NeImBaseResponse.class);
        return responseBean;
    }

    @Override
    public NeImRelationshipResponse getFriendList(NeImFriendBean bean) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("accid", bean.getAccid());
       if (LongUtil.isBlank(bean.getUpdatetime())) {
           params.add("updatetime", Long.toString(new Date().getTime() / 1000));
       } else  {
           params.add("updatetime", Long.toString(bean.getUpdatetime()));
       }
       if (LongUtil.isNotBlank(bean.getCreatetime())) {
           params.add("createtime", Long.toString(bean.getCreatetime()));
       }
        NeImRelationshipResponse relationship = super.build(params, GET_FRIEND_LIST, NeImRelationshipResponse.class);

        return relationship;
    }

    @Override
    public NeImBaseResponse updateUserInfo(NeImUser user) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("accid", user.getAccid());
        params.add("gender", Integer.toString(user.getGender()));
        if (StringUtils.isNotBlank(user.getName())) {
            params.add("name", user.getName());
        }
        if (StringUtils.isNotBlank(user.getIcon())) {
            params.add("icon", user.getIcon());
        }
        if (StringUtils.isNotBlank(user.getSign())) {
            params.add("sign", user.getSign());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            params.add("email", user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getBrith())) {
            params.add("birth", user.getBrith());
        }
        if (StringUtils.isNotBlank(user.getMobile())) {
            params.add("mobile", user.getMobile());
        }
        NeImBaseResponse response = super.build(params, UPDATE_USER_INFO, NeImBaseResponse.class);
        return response;
    }

    @Override
    public NeImUserInfoResponse getUserInfo(String accidJson) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("accids", accidJson);
        NeImUserInfoResponse response = super.build(params, GET_USER_INFO, NeImUserInfoResponse.class);
        return response;
    }
}